package c.francis.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import c.francis.pojo.Category;
import c.francis.service.impl.CategoryServiceImpl;
@Controller
public class CategoryController {
	
	@Autowired
	private CategoryServiceImpl categoryServiceImpl;
	
	@Autowired
	private Category category;
	
	@RequestMapping("/getcategory")
	public ModelAndView showAllCategories() {
		ModelAndView modelAndView = new ModelAndView();
		List<Category> categories = categoryServiceImpl.queryAllCategories();
		modelAndView.addObject("categoryList",categories);
		modelAndView.setViewName("category");
		return modelAndView;
	}
	
	@RequestMapping("/getonecategory")
	public ModelAndView showOneCategory(@RequestParam("code")String code) {
		ModelAndView modelAndView = new ModelAndView();
		Category category = categoryServiceImpl.queryCategoryByCode(code);
		modelAndView.addObject("category", category);
		modelAndView.setViewName("categorydetail");
		return modelAndView;
	}
	
	@RequestMapping("/gomanagecategory")
	public ModelAndView gomanageCategory() {
		ModelAndView modelAndView = new ModelAndView();
		List<Category> categories = categoryServiceImpl.queryAllCategories();
		modelAndView.setViewName("admin_category");
		modelAndView.addObject("categories", categories);
		return modelAndView;
	}
	
	@RequestMapping("getthecategory")
	public ModelAndView getthecategory(@RequestParam("code")String code) {
		ModelAndView modelAndView = new ModelAndView();
		Category category = categoryServiceImpl.queryCategoryByCode(code);
		modelAndView.addObject("category", category);
		modelAndView.setViewName("admin_updatecategory");
		return modelAndView;
	}
	@RequestMapping("updateadmincategory")
	public @ResponseBody int updateCategory(int id,String name,String code,double bedprice,double roomprice,String description) {
		try {
			category.setId(id);
			category.setName(name);
			category.setCode(code);
			category.setBedprice(bedprice);
			category.setRoomprice(roomprice);
			category.setDescription(description);
			categoryServiceImpl.updateCategory(category);
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}
	
}
